package baseTest;

import java.util.HashSet;

public class getIntersectionNode {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(3);
        for (int i = 0; i < 5; i++) {
            listNode.next = new ListNode(i);
        }
        ListNode headB = new ListNode(2);
        headB.next = listNode;
        System.out.println(process(listNode,headB).val);
    }

    public static ListNode process(ListNode headA,ListNode headB){
        /**
         * 错误的解答，该程序可以找出两个链表相同的连续子节点
         */
    //     if (headA == null || headB == null){
    //         return null;
    //     }
    //     ListNode curA = null;
    //     ListNode curB = null;
    //     while (headA != null){
    //         curA = headA;
    //         curB = headB;
    //         while (curB != null){
    //             if (curA.val == curB.val){
    //                 curA = curA.next;
    //                 curB = curB.next;
    //             } else {
    //                 curB = curB.next;
    //             }
    //         }
    //         if (curA == null){
    //             return headA;
    //         }
    //         headA = headA.next;
    //     }
        if (headA == null || headB == null){
            return null;
        }
        HashSet<ListNode> set = new HashSet<>();
        while (headA != null){
            set.add(headA);
            headA = headA.next;
        }
        while (headB != null){
            if (set.contains(headB)){
                return headB;
            }
            headB = headB.next;
        }
         return null;
     }
}
